package me.cxlr.qinlauncher2.view.launcher;

import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import me.cxlr.qinlauncher2.constants.Constants;
import me.cxlr.qinlauncher2.dao.KeyActionDao;
import me.cxlr.qinlauncher2.manager.ActionManager;
import me.cxlr.qinlauncher2.manager.SoftwareManager;
import me.cxlr.qinlauncher2.model.KeyAction;
import me.cxlr.qinlauncher2.util.SharedPreferencesUtil;

/**
 * @author 酷安用户:山轮纸
 */
public class NumberKey {
    private volatile static NumberKey numberKey;

    private NumberKey() {
        keyActionDao = new KeyActionDao();
    }

    public static NumberKey getInstance() {
        if (numberKey == null) {
            synchronized (NumberKey.class) {
                if (numberKey == null) {
                    numberKey = new NumberKey();
                }
            }
        }
        return numberKey;
    }

    private final KeyActionDao keyActionDao;

    /**
     * 数字键短按功能
     *
     * @param keyName 键
     * @param keyType 按键类型
     */
    public boolean numberKey(AppCompatActivity activity, Context context, String keyName, int keyType) {
        boolean flag = SharedPreferencesUtil.getInstance().getBoolean("use_number_key", false);
        if (flag) {
            KeyAction keyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(keyName, keyType);
            switch (keyAction.getActionType()) {
                case Constants.KEY_ACTION_TYPE_OPEN_APP:
                    SoftwareManager.getInstance().openApplication(activity, context, keyAction.getApp().getPackageName(), keyAction.getApp().getClazzName());
                    break;
                case Constants.KEY_ACTION_TYPE_ACTION:
                    ActionManager.getInstance().openAction(activity, context, keyAction.getAction().getActionName());
                    break;
                default:
                    break;
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 数字键短按功能
     *
     * @param keyName 键
     * @param keyType 按键类型
     */
    public boolean numberKeyLongPress(FragmentActivity activity, Context context, String keyName, int keyType) {
        boolean flag = SharedPreferencesUtil.getInstance().getBoolean("use_number_key_long", false);
        if (flag) {
            KeyAction keyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(keyName, keyType);
            switch (keyAction.getActionType()) {
                case Constants.KEY_ACTION_TYPE_OPEN_APP:
                    SoftwareManager.getInstance().openApplication(activity, context, keyAction.getApp().getPackageName(), keyAction.getApp().getClazzName());
                    break;
                case Constants.KEY_ACTION_TYPE_ACTION:
                    ActionManager.getInstance().openAction(activity, context, keyAction.getAction().getActionName());
                    break;
                default:
                    break;
            }
            return true;
        } else {
            return false;
        }
    }
}
